package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

/**
 * @FileName  : ExportQuarantineUpdateThread.java
 * @Project     : 지자체 수출식물검역 및 현장검역(모바일)시스템 구축
 * @Date         : 2014. 8. 27. 
 * @작성자      : pc
 * @변경이력 : 2014. 8. 27. - KJS
 * @프로그램 설명 :
 */
public class ExportQuarantineUpdateThread extends BasicThread{
	
	public interface ExportQuarantineUpdateThreadCallBack {
		public void OnExportQuarantineCompleted(boolean isNetworkConntected, String result
				, String message);
		public void OnExportQuarantineError(ErrorItem errorItem);
	}
	
	private String url;
	private ExportQuarantineUpdateThreadCallBack callback;
	private ArrayList<NameValuePair> post;

	private String master, detail;
	public ExportQuarantineUpdateThread(Context context
			, ExportQuarantineUpdateThreadCallBack callback
			, String master, String detail, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.master = master;
		this.detail = detail;
	}
	
	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.RESULTMASTER, master);
		map.put(Const.RESULTDETAIL, detail);
		String param = "";
		param = parameterReturn(Const.REQ047, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnExportQuarantineCompleted(false, "", "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnExportQuarantineError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			String data = json.getString(Const.DATA);
			if(result == 0){	// result 값이 0일 경우
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				callback.OnExportQuarantineError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				callback.OnExportQuarantineCompleted(true, data, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post =  ParameterUtil.getExportUpdateParams(master, detail);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);
		Log.i("url","url : "+url);
		Log.i("post","post : "+post);
		Log.i("json","json : "+json);*/
		
		

/*		try {		
			
			
		} catch (JSONException e) {
			
		}*/
	}
}
